package com.esunny.resourcevisualization.controller;

import com.esunny.resourcevisualization.domain.entity.User;
import com.esunny.resourcevisualization.domain.response.ReturnStructure;
import com.esunny.resourcevisualization.service.UserService;
import com.esunny.resourcevisualization.util.HostHolder;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * @version 1.0.0.0
 * @ClassName User
 * @Description 用户信息处理接口
 * @Author baochencong
 * @Date 2020/10/9 20:52
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private HostHolder hostHolder;

    @Value("${resourcevisualization.path.domain}")
    private String IP_ADDR;

    @Value("${server.servlet.context-path}")
    private String contextPath;

    /**
     * 通过用户名获取用户信息
     *
     * @return 用户信息
     */
    @ResponseBody
    @GetMapping("/getUserInfo")
    public ReturnStructure getUserInfo() {
        Map<String, Object> map = new HashMap<>(3);
        User userInfo = userService.getUserInfoByUsername(hostHolder.getUser().getUsername());
        if (userInfo.getUserLevel() == 1) {
            map.put("permissions", new String[]{"admin"});
        } else if (userInfo.getUserLevel() == 2) {
            map.put("permissions", new String[]{"group"});
        } else {
            map.put("permissions", new String[]{"editor"});
        }
        map.put("username", userInfo.getRealName());
        map.put("userInfo", userInfo);
        String avatarName = Math.floor(Math.random() * 10 + 1) % 2 == 1 ? "avatar1.gif" : "avatar2.gif";
        map.put("avatar", IP_ADDR + File.separator + contextPath + File.separator + avatarName);
        return ReturnStructure.SUCCESS("获取用户信息成功", map);
    }

    /**
     * 通过用户组名获取用户列表
     *
     * @param userGroupName 用户组名
     * @return 该用户组下用户列表
     */
    @ResponseBody
    @GetMapping("/getUserListByGroupName/{userGroupName}")
    public ReturnStructure getUserListByGroupName(@PathVariable("userGroupName") String userGroupName) {
        if (StringUtils.isBlank(userGroupName)) {
            return ReturnStructure.NOT_FOUND();
        }
        return ReturnStructure.SUCCESS("获取用户列表成功", userService.getUserListByGroupName(userGroupName));
    }

    /**
     * 获取全部用户列表
     *
     * @return 全部用户列表
     */
    @ResponseBody
    @GetMapping("/getUserList")
    public ReturnStructure getUserList() {
        return ReturnStructure.SUCCESS("获取用户列表成功", userService.getUserList());
    }

    /**
     * 通过用户名获取用户信息
     *
     * @param username 用户名
     * @return 用户信息
     */
    @ResponseBody
    @GetMapping("/getUserInfoByUsername/{username}")
    public ReturnStructure getUserInfoByUsername(@PathVariable("username") String username) {
        return ReturnStructure.SUCCESS("获取用户信息成功", userService.getUserInfoByUsername(username));
    }

    /**
     * 更新用户列表
     *
     * @return 更新结果
     */
    @ResponseBody
    @PostMapping("/updateUserList")
    public ReturnStructure updateUserList() {
        userService.updateUserList();
        return ReturnStructure.SUCCESS("更新用户列表成功", null);
    }

}
